<template>
  <div class="mod-config">
      <el-form :inline="true"  v-show="searchShow" ref="searchForm"  :model="dataForm" @keyup.enter.native="searchChange()">
        <el-form-item label="凭证号" prop="billId">
          <el-input v-model="dataForm.billId" placeholder="凭证号"></el-input>
        </el-form-item>

        <el-form-item label="审核状态">
          <el-select v-model="dataForm.status">
            <el-option label="已审核" value="1"></el-option>
            <el-option label="未审核" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="凭证日期">
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="审核日期">
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.auditTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-form-item>
              <el-date-picker type="date" v-model="dataForm.auditTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="制作人" prop="createUserName">
          <el-input v-model="dataForm.createUserName" placeholder="制作人" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="凭证类型" prop="type">
          <el-select v-model="dataForm.type" clearable @change="typeChange">
            <el-option label="付款单" value="1"></el-option>
            <el-option label="收款单" value="2"></el-option>
            <el-option label="记账" value="3"></el-option>
          </el-select>
          <el-select v-model="dataForm.twoType" clearable>
            <el-option label="付款单--发票" :value="1" v-if="dataForm.type == 1"></el-option>
            <el-option label="付款单--单据" :value="2" v-if="dataForm.type == 1"></el-option>
            <el-option label="预付款" :value="3" v-if="dataForm.type == 1"></el-option>
            <el-option label="厂家减账" :value="4" v-if="dataForm.type == 1"></el-option>
            <el-option label="其他付款" :value="5" v-if="dataForm.type == 1"></el-option>
            <el-option label="调账" :value="6" v-if="dataForm.type == 1"></el-option>
            <el-option label="客户批发" :value="7" v-if="dataForm.type == 2"></el-option>
            <el-option label="记账" :value="8" v-if="dataForm.type == 3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商" prop="supplyName">
          <el-input v-model="dataForm.supplyName" readonly>
            <el-button slot="append" @click="checkSupplyList">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="查询部门">
          <el-input v-model="dataForm.deptName" readonly>
            <el-button slot="append" @click="openDeptDialog">选择</el-button>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>

      </el-form>
      <el-row>
      <el-col :span="24">
        <el-button v-if="permissions.eims_evidence_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(0,1)">凭证新增</el-button>
        <el-button v-if="permissions.eims_evidence_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle(0,2)">记账凭证新增</el-button>
        <el-button icon="el-icon-search" circle @click="handleSearchShow" style="float:right;"></el-button>
        <el-button icon="el-icon-refresh" circle @click="refreshChange" style="float:right;"></el-button>
        <!-- <el-button icon="el-icon-menu" circle size="mini" style="float:right;"></el-button>-->

      </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column prop="billId" label="单号"></el-table-column>
        <el-table-column prop="status" label="审核状态" :formatter="statusFormatter"></el-table-column>
        <el-table-column prop="evidenceType" label="凭证类型" :formatter="evidenceTypeFormatter"></el-table-column>
        <el-table-column prop="billType" label="单据种类" :formatter="billTypeFormatter"></el-table-column>
        <el-table-column prop="attachedNumber" label="附单数"></el-table-column>
        <el-table-column prop="memo" label="备注"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" @click="getInfo(scope.row)">详情</el-button>
              <el-button v-if="permissions.eims_evidence_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row,3)" :disabled="scope.row.status == '1'">修改</el-button>
              <el-button v-if="permissions.eims_evidence_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)" :disabled="scope.row.status == '1'">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <!-- 凭证弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
      <!-- 记账凭证弹窗, 新增 / 修改 -->
      <tally-evidence v-if="tallyAddOrUpdateVisible" ref="tallyAddOrUpdate" @refreshDataList="getDataList"></tally-evidence>
      <!-- 凭证弹窗, 详情 -->
      <detail-form v-if="getInfoVisible1" ref="getInfo1" @refreshDataList="getDataList"></detail-form>
      <!-- 记账凭证弹窗, 详情 -->
      <tally-detail v-if="getInfoVisible2" ref="getInfo2" @refreshDataList="getDataList"></tally-detail>
      <!-- 弹窗, 制造商查询 -->
      <supply-search v-if="supplySearchVisible" ref="supplySearch" @refreshData="renewFormData"></supply-search>
      <!-- 弹窗,选择部门 -->
      <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
  </div>
</template>

<script>
  import {fetchList, delObj} from '@/api/eims/evidence'
  import TableForm from './evidence-form'
  import TallyEvidence from './tally-evidence-form'
  import DetailForm from './evidence-detail'
  import TallyDetail from './tally-detail'
  import {mapGetters} from 'vuex'
  import SupplySearch from "@/components/business/SearchSupply";
  import SearchDept from './SearchDept'
  import { filterParams } from "@/util/tools";
  export default {
    data () {
      return {
        dataForm: {
          supplyName: '',
          deptName: ''
        },
        supplySearchVisible: false,
        tallyAddOrUpdateVisible: false,
        deptVisible: false,
        dataList: [],
        searchShow:true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        getInfoVisible1: false,
        getInfoVisible2: false
      }
    },
    components: {
      TableForm,
      SupplySearch,
      SearchDept,
      TallyEvidence,
      DetailForm,
      TallyDetail
    },
    created () {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        fetchList(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (row,changeType) {
        if(changeType==1){
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(0)
          })
        }else if(changeType==2){
          this.tallyAddOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.tallyAddOrUpdate.init(0)
          })
        }else if(changeType==3){
          if(row.evidenceType==1){
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
              this.$refs.addOrUpdate.init(row.id)
            })
          }else if(row.evidenceType==2){
            this.tallyAddOrUpdateVisible = true
            this.$nextTick(() => {
              this.$refs.tallyAddOrUpdate.init(row.id)
            })
          }
        }

      },
      //详情
      getInfo(row){
        if(row.evidenceType==1){
          this.getInfoVisible1 = true
          this.$nextTick(() => {
            this.$refs.getInfo1.init(row.id)
          })
        }else if(row.evidenceType==2){
          this.getInfoVisible2 = true
          this.$nextTick(() => {
            this.$refs.getInfo2.init(row.id)
          })
        }
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex=1;
        this.getDataList()
      },
      // 搜索清空
      searchReset () {
        for(let key in this.dataForm){
            this.dataForm[key] = ''
          }
      },
      handleSearchShow () {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      //凭证类型改变
      typeChange(){
        if(this.dataForm.type==1){
          this.dataForm.twoType =1
        }else if(this.dataForm.type==2){
          this.dataForm.twoType =7
        }else if(this.dataForm.type==3){
          this.dataForm.twoType =8
        }else{
          this.dataForm.twoType = ''
        }
      },
      // 选择制造商
      checkSupplyList() {
        let data = {};
        this.supplySearchVisible = true;
        this.$nextTick(() => {
          this.$refs.supplySearch.init(data);
        });
      },
      renewFormData(data) {
        this.dataForm.supplyId = data.id;
        this.dataForm.supplyName = data.name;
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      //  获取选择部门数据
      getDeptData(data) {
        this.dataForm.deptName = data.name
        this.dataForm.deptId = data.id
      },
      statusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '0':
            return '未审核'
            break
          case '1':
            return '已审核'
            break
        }
      },
      evidenceTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '1':
            return '普通凭证'
            break
          case '2':
            return '记账凭证'
            break
        }
      },
      billTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case '1':
            return '制造商'
            break
          case '2':
            return '部门'
            break
          case '3':
            return '客户'
            break
        }
      },
    }
  }
</script>
